<?php

require_once '../util.php';
require_once '../config.php';

connect_db_mute();

function add_retweet_task($tweet_id, $user_ids, $all_retweet_content, $tweet_info) {
    $tweet_id = mysql_real_escape_string($tweet_id);
    $added_task_count = 0;
    $last_content = end($all_retweet_content);
    $account_key = 0;
    foreach($user_ids as $user_id) {
        $user_id = mysql_real_escape_string($user_id);
        $retweet_content = $all_retweet_content[$account_key];
        $added_task_content = "tweet_id=$tweet_id&retweet_content=$retweet_content&user_id=$user_id&$tweet_info";
        $sql = sprintf("insert into task(user_id, type, content) values ('%s', 'retweet', '%s')",
                       $user_id, $added_task_content);
        $ret = sql_or_die($sql);
        $added_task_count += mysql_affected_rows();
        if ($retweet_content == $last_content)
            $account_key = 0;
        else
            $account_key = $account_key + 1;
  }
  return $added_task_count;
}

$user_ids = $_POST["user_ids"];
$tweet_id = $_POST["tweet_id"];
$all_retweet_content = $_POST["retweet_content"];
$tweet_info = $_POST["tweet_info"];

$added_task_count = add_retweet_task($tweet_id, $user_ids, $all_retweet_content, $tweet_info);
echo "successfully updated $added_task_count rows";

?>
